package com.jsgt.greekfire.ui.home.adapter

import android.view.View
import android.view.ViewGroup
import androidx.viewpager.widget.PagerAdapter

/**
 * 通用的ViewPager Adapter
 *
 * @author liaody
 */
class CommonViewPageAdapter(var viewList: List<View>?) :
    PagerAdapter() {
    private var pageTitles: List<String>? = null

    fun setPageTitles(pageTitles: List<String>?) {
        this.pageTitles = pageTitles
    }

    override fun destroyItem(
        container: ViewGroup,
        position: Int,
        obj: Any
    ) {
        container.removeView(viewList!![position]) // 删除页卡
    }

    override fun instantiateItem(
        container: ViewGroup,
        position: Int
    ): Any { // 这个方法用来实例化页卡
        container.addView(viewList!![position], 0) // 添加页卡
        return viewList!![position]
    }

    override fun getCount(): Int {
        return if (viewList != null) viewList!!.size else 0 // 返回页卡的数量
    }

    override fun isViewFromObject(
        arg0: View,
        arg1: Any
    ): Boolean {
        return arg0 === arg1
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return if (pageTitles != null) pageTitles!![position] else null
    }

}